How much cash? 50000 How much credit do you have? 75000 Enough to buy this car!
When execution gets to the if
statement, it finds that
cash >= 25000 --- is true, because 50000 >= 25000
also that
credit >= 25000 --- is true, because 75000 >= 25000
All that OR needs is a single true, so the above boolean expression is true.
The or-operator is used in a boolean expression to check that there is at least one true. If both sides are true, the entire expression is true. If just one side is true, the entire expression is true. If both sides are false, the entire expression is false. The or-operator is a logical operator because it combines two true/false values into a single true/false value.
Here is how ||
works:
true || true = true
false || true = true
true || false = true
false || false = false
OR checks that at least one requirement is met. This type of OR is called an inclusive OR because its value is true for one or two true values.
Sometimes in English the word "or" is used when only one condition can be true at a time. For example in this sentence
It will rain today or it will be clear.
only one condition, "rain" or "clear", can be true.
This is called an exclusive OR.
The Java ||
is always an inclusive OR.
Here is a boolean expression:
34 > 2 || 5 == 7
Is this expression true or false ?